package com.petalsgroup.petals.service.admin.impl;

import com.petalsgroup.petals.dao.admin.AdminDao;
import com.petalsgroup.petals.dao.admin.impl.AdminDaoImpl;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.service.admin.UserAdminService;
import com.petalsgroup.petals.util.LimitUtil;
import com.petalsgroup.petals.util.MailUtils;

import java.util.List;

public class UserAdminServiceIpml implements UserAdminService {
    AdminDao ad=new AdminDaoImpl();
    /**
     * 分页获取用户信息列表
     * @param userAdminLimit
     * @author wgd
     * @return
     */
    @Override
    public List<UserDO> findAllUserByPages(LimitUtil userAdminLimit) {
        long len = ad.countUser();
        if (len == 0) {
            return null;
        }
        if (userAdminLimit.getCurrentPage()==null){
            userAdminLimit.init(len, 4, 1);
        }
        List<UserDO> userList = ad.listUser(userAdminLimit.getSelect(), userAdminLimit.getNum());
        if (userList == null) {
            return null;
        }
        return userList;
    }

    /**
     * 根据uid禁言用户
     * @param uid
     * @author wgd
     */
    @Override
    public void bannedUser(String uid) {
        String email=ad.findUserEmailByUid(uid);
        MailUtils.sendMail(email,"<p>由于您在花瓣漫评网上的不正当操作，管理员已经对您进行相关操作，您已无法发表评论！<p>","花瓣漫评通知");
        ad.bannedUser(uid);
    }

    /**
     * 解禁用户
     * @param uid
     * @author wgd
     */
    @Override
    public void resolveUser(String uid) {
        ad.resolveUser(uid);

    }

    /**
     * 根据id封号用户
     * @param uid
     * @author wgd
     */
    @Override
    public void deleteUser(String uid) {
        ad.deleteUser(uid);
        String email=ad.findUserEmailByUid(uid);
        MailUtils.sendMail(email,"<p>由于您在花瓣漫评网上的违规操作，您的账号已被永久封禁！<p>","花瓣漫评通知");
    }

    /**
     * 禁言选中的用户
     * @param uids
     * @author wgd
     */
    @Override
    public void bannedSelectedUser(String[] uids) {
        if (uids != null && uids.length > 0){
            //1.遍历获取用户信息
            for (String uid: uids) {
                //2.调用dao中的禁言方法
                String email=ad.findUserEmailByUid(uid);
                //MailUtils.sendMail(email,"<p>由于您在花瓣漫评网上的不正当操作，管理员已经对您进行相关操作，您已无法发表评论！<p>","花瓣漫评通知");
                ad.bannedUser(uid);
            }
        }
    }

    /**
     * 封号选中的用户
     * @param uids
     * @author wgd
     */
    @Override
    public void deleteSelectUser(String[] uids) {
        if (uids != null && uids.length > 0){
            //1.遍历获取用户信息
            for (String uid: uids) {
                //2.调用dao中的封号方法
                String email=ad.findUserEmailByUid(uid);
                //MailUtils.sendMail(email,"<p>由于您在花瓣漫评网上的违规操作，您的账号已被永久封禁！<p>","花瓣漫评通知");
                ad.deleteUser(uid);
            }
        }
    }

    /**
     * 解禁选中的用户
     * @param uids
     * @author wgd
     */

    @Override
    public void resolveSelectUser(String[] uids) {
        if (uids != null && uids.length > 0){
            //1.遍历获取用户信息
            for (String uid: uids) {
                //2.调用dao中的解禁方法
                ad.resolveUser(uid);
            }
        }
    }

    /**
     * 根据邮箱/用户名/姓名中是否包含该信息查询用户
     * @param userInfo
     * @author wgd
     * @return
     */
    @Override
    public List<UserDO> findUserByUsernameNameEmail(String userInfo) {
        List<UserDO> list=ad.findUserByUsernameNameEmail(userInfo);
        return list;
    }
    /**
     * 查询被禁言的用户
     * @author wgd
     * @return
     */
    @Override
    public List<UserDO> findBannedUser() {
        List<UserDO> list=ad.findBannedUser();
        return list;
    }

    /**
     * 根据id查询用户
     * @param userInfo
     * @return
     */
    @Override
    public List<UserDO> findUserById(String userInfo) {
        List<UserDO> list=ad.findUserById(userInfo);
        return list;
    }

    /**
     * 根据用户名查询用户
     * @param userInfo
     * @return
     */
    @Override
    public List<UserDO> findUserByUsername(String userInfo) {
        List<UserDO> list=ad.findUserByUsername(userInfo);
        return list;
    }

    /**
     * 根据邮箱查询用户
     * @param userInfo
     * @return
     */
    @Override
    public List<UserDO> findUserByEmail(String userInfo) {
        List<UserDO> list=ad.findUserByEmail(userInfo);
        return list;
    }
}
